package dkbta.ontology.elements.abstractions.trend;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import dkbta.ontology.elements.Element;
import dkbta.ontology.elements.ElementType;
import dkbta.ontology.elements.abstractions.Abstraction;

public class Trend extends Abstraction{
	private final Element _first;

	private Element _last;

	/**
	 * Initializes a trend instance with the given parameters and no first/last primitives
	 * 
	 * @param name The trend's name
	 * @param startTime The trend's start time
	 * @param endTime The trend's end time
	 * @param value The value of the trend
	 */
	public Trend(String name, long startTime, long endTime, Object value){
		super(ElementType.TREND, name, startTime, endTime, value);
		_first = null;
		_last = null;
	}

	/**
	 * Initializes a trend instance with the given parameters
	 * 
	 * @param name The trend's name
	 * @param startTime The trend's start time
	 * @param endTime The trend's end time
	 * @param value The value of the trend
	 * @param firstPrimitive The primitive from which the trend starts
	 * @param lastPrimitive The primitive where the trend ends
	 */
	public Trend(String name, long startTime, long endTime, Object value,
		Element firstPrimitive, Element lastPrimitive){
		super(ElementType.TREND, name, startTime, endTime, value);
		_first = firstPrimitive;
		_last = lastPrimitive;
	}

	@Override
	public void insertDataIntoPS(PreparedStatement ps, int valueIndex, int typeIndex)
			throws SQLException{
		String value = "SAME";
		if ("Increasing".equalsIgnoreCase((String)_value)){
			value = "INC";
		}else if ("Decreasing".equalsIgnoreCase((String)_value)){
			value = "DEC";
		}
		ps.setString(valueIndex, value);
		ps.setString(typeIndex, "TREND");
	}

	public Element getFirst(){
		return _first;
	}

	public Element getLast(){
		return _last;
	}

	public void setLast(Element last){
		_last = last;
		_timeInterval.setEndTime(last.getTimeInterval().getEndTime());
	}

	public void setValue(String value){
		_value = value;
	}
}
